import { Injectable } from '@nestjs/common';
// import { extname } from 'path';
import * as fs from 'fs';
import { app } from 'electron';
import path from 'path';

@Injectable()
export class FileService {
  async uploadFile(multipart): Promise<string> {
    const fileName = multipart['file'][0].filename;
    const data = multipart['file'][0].data;
    //let encoding = multipart['editormd-image-file'][0].encoding

    const destinationPath = app.getPath('userData') + '/UploadFile';
    // 获取当前日期并格式化为 yyyy-mm-dd 的字符串
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, '0');
    const day = String(currentDate.getDate()).padStart(2, '0');
    const dateFolder = `${year}-${month}-${day}`;

    // 确保目标目录存在
    const destinationFolder = path.join(destinationPath, dateFolder);
    if (!fs.existsSync(destinationFolder)) {
      fs.mkdirSync(destinationFolder, { recursive: true });
    }

    const name = fileName.substring(0, fileName.lastIndexOf('.'));

    const fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);

    const newFilePath = `${name}&${new Date().getTime()}.${fileExtension}`;

    // 拼接目标文件路径
    const destinationFile = path.join(destinationFolder, newFilePath);

    const writerStream = fs.createWriteStream(destinationFile);

    writerStream.write(data);

    await writerStream.end();

    // 读取源文件内容并写入目标文件
    // const data = fs.readFileSync(filePath);
    // fs.writeFileSync(destinationFile, data);

    console.log('File copied successfully');

    //文件上传路径
    // let path = join(__dirname, '../static/upload/');

    // const writerStream = fs.createWriteStream(
    //     path + filename
    // );
    // writerStream.write(data);
    // await writerStream.end();

    return `${dateFolder}/${newFilePath}`;
  }

  async uploadInterface(multipart): Promise<string> {
    const fileName = multipart['file'][0].filename;
    const data = multipart['file'][0].data;
    //let encoding = multipart['editormd-image-file'][0].encoding

    const destinationPath = app.getPath('userData') + '/UploadFile';
    // 获取当前日期并格式化为 yyyy-mm-dd 的字符串
    // const currentDate = new Date();
    // const year = currentDate.getFullYear();
    // const month = String(currentDate.getMonth() + 1).padStart(2, '0');
    // const day = String(currentDate.getDate()).padStart(2, '0');
    const dateFolder = `uploadInterface`;

    // 确保目标目录存在
    const destinationFolder = path.join(destinationPath, dateFolder);
    if (!fs.existsSync(destinationFolder)) {
      fs.mkdirSync(destinationFolder, { recursive: true });
    }

    const name = fileName.substring(0, fileName.lastIndexOf('.'));

    const fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);

    const newFilePath = `${name}&${new Date().getTime()}.${fileExtension}`;

    // 拼接目标文件路径
    const destinationFile = path.join(destinationFolder, newFilePath);

    const writerStream = fs.createWriteStream(destinationFile);

    writerStream.write(data);

    await writerStream.end();

    // 读取源文件内容并写入目标文件
    // const data = fs.readFileSync(filePath);
    // fs.writeFileSync(destinationFile, data);

    console.log('File copied successfully');

    //文件上传路径
    // let path = join(__dirname, '../static/upload/');

    // const writerStream = fs.createWriteStream(
    //     path + filename
    // );
    // writerStream.write(data);
    // await writerStream.end();

    return `${dateFolder}/${newFilePath}`;
  }

  async unlinkInterface(data): Promise<string> {
    const destinationPath = app.getPath('userData') + '/UploadFile';
    const dateFolder = `uploadInterface`;
    // 确保目标目录存在
    const destinationFolder = path.join(destinationPath, dateFolder);
    // 拼接目标文件路径
    const imagePath = path.join(destinationFolder, data);

    fs.unlink(imagePath, (err) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log('图片文件已成功删除');
    });

    console.log('unlink successfully');

    return `unlink successfully`;
  }
}
